import csv
import json
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import matplotlib.dates as mdates

from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score
from pandas import read_csv
from pandas import DataFrame
from pandas import concat
from sklearn import preprocessing
from statsmodels.graphics.tsaplots import plot_acf, plot_pacf
import seaborn as sns
# plt.rcParams['font.sans-serif'] = ['KaiTi']  # 指定默认字体
# plt.rcParams['axes.unicode_minus'] = False  # 解决保存图像是负号'-'显示为方块的问题
font = {'family': 'KaiTi',
        'weight': 'normal',
        'size': 20,
        }
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
sns.set_context("paper")

def draw_data_paper(pre_data, true_data, picture_path, title):
    fig, ax = plt.subplots(1, 1, figsize=(9, 6))
    ax.plot(true_data, color='k', label='实际值', linestyle='-')
    ax.plot(pre_data, color='k', label='预测值', linestyle='--')
    ax.set_xlabel('时序', fontsize=20)
    ax.set_ylabel('客流量', fontsize=20)
    ax.tick_params(labelsize=20)
    ax.legend(fontsize=20)
    sns.despine()  # 去掉右框线和上框线
    plt.savefig(picture_path)
    plt.show()

if __name__ == '__main__':
    helper = pd.DataFrame(
        {'time_t': pd.date_range(start='07:30:00', end='23:00:00', freq='10min')})
    helper['time']=helper['time_t'].dt.time
    print(helper['time'])
    true=np.load("./result/in/test.npy")

    bpnn=np.load("./result/in/bp.npy")

    svr=np.load("./result/in/svr.npy")

    arima=np.load("./result/in/arima.npy")

    lstm=np.load("./result/in/lstm.npy")
    att=np.load("./result/in/att.npy")

    true=np.load("./result/out/test.npy")
    bpnn = np.load("./result/out/lstm.npy")
    svr = np.load("./result/out/svr.npy")
    arima = np.load("./result/out/arima.npy")
    lstm = np.load("./result/out/bp.npy")
    att = np.load("./result/out/att.npy")

    mse_bp = mean_squared_error(true, bpnn)
    mae_bp = mean_absolute_error(true, bpnn)
    r2_bp = r2_score(true, bpnn)

    mse_svr = mean_squared_error(true, svr)
    mae_svr = mean_absolute_error(true, svr)
    r2_svr = r2_score(true, svr)

    mse_arima = mean_squared_error(true, arima)
    mae_arima = mean_absolute_error(true, arima)
    r2_arima = r2_score(true, arima)

    mse_lstm = mean_squared_error(true, lstm)
    mae_lstm = mean_absolute_error(true, lstm)
    r2_lstm = r2_score(true, lstm)

    mse_att = mean_squared_error(true, att)
    mae_att = mean_absolute_error(true, att)
    r2_att = r2_score(true, att)

    print("bpnn: mse:",mse_bp,"mae:",mae_bp)
    # print("svr: mse:", mse_svr, "mae:", mae_svr)
    print("arima: mse:", mse_arima, "mae:", mae_arima)
    print("lstm: mse:", mse_lstm, "mae:", mae_lstm)
    print("att: mse:", mse_att, "mae:", mae_att)

    fig = plt.figure(figsize=(10, 5))
    plt.rcParams['xtick.direction'] = 'in'
    plt.rcParams['ytick.direction'] = 'in'

    colors = ['#1f77b4', '#ff7f0e', '#2ca02c', '#d62728', '#9467bd',  # 使用颜色编码定义颜色
              '#8c564b', '#e377c2', '#7f7f7f', '#bcbd22', '#17becf']

    plt.plot(helper['time_t'],true,label='true',color=colors[0])



    plt.plot(helper['time_t'],bpnn,label='BPNN',color=colors[4],linestyle='--')
    plt.plot(helper['time_t'],arima,label='ARIMA',color=colors[2],linestyle='--')
    plt.plot(helper['time_t'],lstm,label='LSTM',color=colors[1],linestyle='--')
    plt.plot(helper['time_t'],att,label='TFATT',color=colors[3])

    plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%H:%M:%S'))
    plt.xlabel('时刻',fontsize=10)
    plt.ylabel('客流量 人次/10min',fontsize=10)
    plt.legend()
    plt.show()

